package com.lancoo.answer.widget.combinationView;

import android.content.Context;
import android.text.Html;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lancoo.answer.R;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Item;
import com.lancoo.answer.util.ExamFontSizeAdjustUtils;
import com.lancoo.answer.util.RichTextUtils;

import java.util.List;

/**
 * Created by pengmi on 2021/12/15 13:42
 * Description:作文题原题View(参考答案 ，解析)
 */
public class CompositionOriginalView extends RelativeLayout {

    private static final String TAG = "CompositionOriginalView";

    public CompositionOriginalView(Context context) {
        super(context);
        init(context);
    }

    public CompositionOriginalView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CompositionOriginalView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context){
        LayoutInflater.from(context).inflate(R.layout.ev_layout_composition_answer_result,this);
        TextView tv_title=findViewById(R.id.tv_score_des);
        TextView tv_title1=findViewById(R.id.tv_answer_des);
        TextView tv_title2=findViewById(R.id.tv_my_answer_des);
        TextView tv_title3=findViewById(R.id.tv_my_score);
        TextView tv_title4=findViewById(R.id.tv_ev_des);
        String str="【";
        float v = tv_title.getPaint().measureText(str);
        tv_title.setTranslationX(-v/2);
        tv_title1.setTranslationX(-v/2);
        tv_title2.setTranslationX(-v/2);
        tv_title3.setTranslationX(-v/2);
        tv_title.setVisibility(GONE);
        tv_title3.setVisibility(GONE);
        tv_title2.setVisibility(GONE);
        tv_title4.setVisibility(GONE);
        findViewById(R.id.rl_my_answer).setVisibility(GONE);
        LayoutParams layoutParams = (LayoutParams) tv_title1.getLayoutParams();
        layoutParams.topMargin=0;
        tv_title1.setLayoutParams(layoutParams);
        adjustFontSize();

    }


    public void setChild(Child child,String genreID){
        Log.e(TAG, "setChild: "+"CompositionOriginalView,setChild"+genreID);
        AnswerAnalysisView answerAnalysisView = findViewById(R.id.answer_analysis_view);
        StringBuffer analysis=new StringBuffer();
        StringBuffer answer=new StringBuffer();
        List<Item> itemList = child.getItemList();
        if(itemList==null){
            return;
        }
        for (Item item:itemList){
            if(!TextUtils.isEmpty(analysis)&&!TextUtils.isEmpty(item.getItemAnalysis())){
                analysis.append("<br>");
            }
            analysis.append(item.getItemAnalysis());
            if(!TextUtils.isEmpty(answer)&&!TextUtils.isEmpty(item.getItemAnswer())){
                answer.append("<br>");
            }
            answer.append(item.getItemAnswer());
        }
        answerAnalysisView.setAnalysis(analysis.toString());
        answerAnalysisView.setAlignmentType(1);
        TextView tv_answer=findViewById(R.id.tv_answer);
        String childAnswer =answer.toString();
        if(TextUtils.isEmpty(childAnswer)){
            tv_answer.setText(R.string.ev_item_null);
        }else{
            boolean isNeedTr=TextUtils.equals("52",genreID)||TextUtils.equals("34",genreID);
            boolean isIndentChildAnswer = child.getIsIndentChildAnswer();
            tv_answer.setText(RichTextUtils.getQueseAnswerStr(childAnswer,!isNeedTr&&isIndentChildAnswer,false));
        }

        TextView textView=findViewById(R.id.tv_child_answer_count);
        SpannableStringBuilder spannableStringBuilder=new SpannableStringBuilder();
        spannableStringBuilder.append("Word Count:");
        int count=0;
        String childAnswerRichTxt= Html.fromHtml(childAnswer).toString();
        if(!TextUtils.isEmpty(childAnswerRichTxt)){
            String[] split = childAnswerRichTxt.split(" ");
            count=split.length;
        }
        SpannableString spannableString=new SpannableString(""+count);
        spannableString.setSpan(new ForegroundColorSpan(0xff666666),0,spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.append(spannableString);
        textView.setText(spannableStringBuilder);

    }


    public void adjustFontSize(){
        TextView textView=findViewById(R.id.tv_child_answer_count);
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(textView,13);
        TextView tv_answer=findViewById(R.id.tv_answer);
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(tv_answer,13);
        AnswerAnalysisView answerAnalysisView = findViewById(R.id.answer_analysis_view);
        answerAnalysisView.adjustFontSize();
    }


}
